package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.Detailed_Travel_area_Activity;
import com.itcast.donkey.modles.DetailedJourneyModles;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 16/9/1.
 */
public class TextListRecyclerAdapter extends RecyclerView.Adapter<TextListRecyclerAdapter.TextViewHolder> {

    private Context context;
    private DetailedJourneyModles.PlanDaysBean daysBean;
    private int count;

    public TextListRecyclerAdapter(Context context, DetailedJourneyModles.PlanDaysBean daysBean) {
        this.context = context;
        this.daysBean = daysBean;
        this.count = daysBean.getPlan_nodes().size() + 1;

    }

    @Override
    public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_textlistview, null);
        TextViewHolder holder = new TextViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(TextViewHolder holder, int position) {
        if (position == 0) {
            holder.linTitle.setVisibility(View.VISIBLE);
            holder.tvItemTextlistContent.setVisibility(View.VISIBLE);
            holder.tvItemTextlistContent.setText(daysBean.getMemo());
        } else {
            final int i = position - 1;
            if (daysBean.getPlan_nodes().get(i).isCandidate()) {
                holder.lineItemTextlistClick.setVisibility(View.VISIBLE);
                holder.tvItemTextlistContent.setVisibility(View.VISIBLE);
                holder.tvItemTextlistTitle.setText("备选" + daysBean.getPlan_nodes().get(i).getEntry_name());
                holder.tvItemTextlistContent.setText(daysBean.getPlan_nodes().get(i).getTips());
                holder.ivItemTextlistImage.setVisibility(View.GONE);
            } else {
                holder.lineItemTextlistClick.setVisibility(View.VISIBLE);
                holder.tvItemTextlistContent.setVisibility(View.VISIBLE);
                int x = daysBean.getPlan_nodes().get(i).getPosition() + 1;
                holder.tvItemTextlistTitle.setText("第" + x + "站:" + daysBean.getPlan_nodes().get(i).getEntry_name());
                Picasso.with(context).load(daysBean.getPlan_nodes().get(i).getImage_url()).into(holder.ivItemTextlistImage);
                holder.tvItemTextlistContent.setText(daysBean.getPlan_nodes().get(i).getTips());
            }
            holder.lineItemTextlistClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, Detailed_Travel_area_Activity.class);
                    intent.putExtra("id",daysBean.getPlan_nodes().get(i).getEntry_id());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class TextViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout linTitle;
        private LinearLayout lineItemTextlistClick;
        private TextView tvItemTextlistTitle;
        private ImageView ivItemTextlistImage;
        private TextView tvItemTextlistContent;

        public TextViewHolder(View itemView) {
            super(itemView);
            linTitle = (LinearLayout) itemView.findViewById(R.id.lin_title);
            lineItemTextlistClick = (LinearLayout) itemView.findViewById(R.id.line_item_textlist_click);
            tvItemTextlistTitle = (TextView) itemView.findViewById(R.id.tv_item_textlist_title);
            ivItemTextlistImage = (ImageView) itemView.findViewById(R.id.iv_item_textlist_image);
            tvItemTextlistContent = (TextView) itemView.findViewById(R.id.tv_item_textlist_content);
        }
    }
}
